from fake_useragent import UserAgent
import urllib.request
import requests
import json
import pymysql

url = 'http://china.nba.com/static/data/league/teamstats_All_All_2017_4.json'
ua = UserAgent()
headers={}
headers['User-Agent'] = ua.random
#一次get获取(str)
r = requests.get(url,headers = headers).text
payload = json.loads(r)['payload']
#三次列表选取(list)
dataLists = payload['teams']
for dataList in dataLists:
    profile = dataList['profile']
    # newsObjs = [profile.get('name'),profile.get('code'),profile.get('city')]
    # print(type(newsObjs))
#     #连接数据库
#     conn = pymysql.connect(host="localhost", user="root",
#                          password="root", db="nba", port=3306,charset = 'utf8')
#     # 使用cursor()方法获取操作游标
#     cur = conn.cursor()
#     #编写sql语句
#     # (name,code,city)
#     sql = "INSERT INTO test(name,code,city) VALUES (%s,%s,%s)"
#     effect = cur.execute(sql,newsObjs)
#     conn.commit()
# # 关闭游标
# cur.close()
# # 关闭连接
# conn.close()